package com.example.scaffold_demo.db;

import android.content.Context;

import com.example.scaffold_demo.db.greendao.DaoMaster;
import com.github.yuweiguocn.library.greendao.MigrationHelper;

import org.greenrobot.greendao.database.Database;


/**
 * Author: 李巷阳
 * Date: 2020/6/15
 * Part:
 * Description:
 */
public class DatabaseOpenHelper  extends DaoMaster.OpenHelper{
    public DatabaseOpenHelper(Context context, String name) {
        super(context, name);
    }


    /**
     * 数据库表,有字段修改,需要在这进行声明.
     * @param db
     * @param oldVersion
     * @param newVersion
     */
    @Override
    public void onUpgrade(Database db, int oldVersion, int newVersion) {
        if (oldVersion < newVersion) {
            //把需要管理的数据库表DAO作为最后一个参数传入到方法中
            MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {

                @Override
                public void onCreateAllTables(Database db, boolean ifNotExists) {
                    DaoMaster.createAllTables(db, ifNotExists);
                }

                @Override
                public void onDropAllTables(Database db, boolean ifExists) {
                    DaoMaster.dropAllTables(db, ifExists);
                }
            });
        }
    }

}
